﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		TagService.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 // 
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion
 
using System;
using System.Collections.Generic;
using Dexter.Core.EventArgs;
using Dexter.DataTransferObject;
using Dexter.Domain.Model;
using Dexter.Domain.Results;
using Dexter.Logger.Contracts;
using Dexter.Repository;
using Dexter.Services;

namespace Dexter.Services.Implementation {
	/// <summary>
	/// 	The implementation of <see cref = "ITagService" />.
	/// </summary>
	public class TagService : ITagService {
		readonly ILogger logger;
		readonly ITagRepository tagRespository;

		/// <summary>
		/// 	Initializes a new instance of the <see cref = "TagService" /> class.
		/// </summary>
		/// <param name = "logger">The logger.</param>
		/// <param name = "tagRespository">The tag respository.</param>
		public TagService ( ILogger logger , ITagRepository tagRespository ) {
			this.logger = logger;
			this.tagRespository = tagRespository;
		}

		///<summary>
		///	Retrieve a list of <see cref = "Post" /> with the specific <paramref name = "tag" />
		///</summary>
		///<param name = "tag">The tag.</param>
		///<param name = "pageIndex">Index of the page (0 based).</param>
		///<param name = "pageSize">The number of items to retrieve.</param>
		///<remarks>
		///	The result will include only published and past posts, unpublished, future will be excluded from this result.
		///	Moreover the result hasn't the tracking system enabled
		///</remarks>
		///<returns>An concrete implementation of <see cref = "IPagedResult{T}" />. The result will never be null.</returns>
		public virtual IPagedResult<Post> GetPostByTag ( string tag, int pageIndex, int pageSize ) {
			var eao = new Tuple <string , int , int> ( tag , pageIndex , pageSize );
			var ea = new CancelEventArgsWithOneParameter <Tuple <string , int , int>> ( eao );

			PostReceiving.Raise ( this , ea );

			if ( ea.Cancel ) {
				return new EmptyPagedResult <Post> ( pageIndex , pageSize );
			}

			var data = tagRespository.GetPostsByTag ( pageIndex , pageSize , tag , false , false );

			PostReceived.Raise ( this , new GenericEventArgs <IPagedResult <Post>> ( data ) );

			return data;
		}

		public IPagedResult<PostDto> GetPostDtoByTag(string tag, int pageIndex, int pageSize)
		{
			return GetPostByTag(tag, pageIndex, pageSize).ToDtos();
		}

		///<summary>
		///	Retrieve the top used tags for all approved items.
		///</summary>
		///<param name = "numberOfItemsToRetireve">The number of items to retireve.</param>
		///<returns>
		///	Return a list of <see cref = "TagDto" />.
		///	The return item could be null if there isn't items into the repository.
		///</returns>
		public virtual IList<TagDto> GetTopTags(int numberOfItemsToRetireve)
		{
			var ea = new CancelEventArgsWithOneParameter <int> ( numberOfItemsToRetireve );

			TagReceiving.Raise ( this , ea );

			if ( ea.Cancel ) {
				return null;
			}

			var data = tagRespository.GetTopTags ( numberOfItemsToRetireve , false , false );

			TagReceived.Raise(this, new GenericEventArgs<IList<TagDto>>(data));

			return data;
		}

		/// <summary>
		/// 	This event will raise before a set of  <see cref = "Post" /> will be retrieved by the implementation of <see cref = "GetPostByTag(string, int, int)" />.
		/// </summary>
		public virtual event EventHandler<CancelEventArgsWithOneParameter<Tuple<string, int, int>>> PostReceiving;

		/// <summary>
		/// 	This event will raise after a set of <see cref = "Post" /> will be retrieved by the implementation of <see cref = "GetPostByTag(string, int, int)" />.
		/// </summary>
		public virtual event EventHandler<GenericEventArgs<IPagedResult<Post>>> PostReceived;

		/// <summary>
		/// 	This event will raise before a <see cref = "TagDto" /> will be saved by the implementation of <see cref = "GetTopTags(int)" />.
		/// </summary>
		public virtual event EventHandler<CancelEventArgsWithOneParameter<int>> TagReceiving;

		/// <summary>
		/// 	This event will raise after a <see cref = "TagDto" /> will be saved by the implementation of<see cref = "GetTopTags(int)" />.
		/// </summary>
		public virtual event EventHandler<GenericEventArgs<IList<TagDto>>> TagReceived;
	}
}
